package com.heihx.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heihx.dto.*;
import com.heihx.entity.Orders;
import com.heihx.result.PageResult;
import com.heihx.result.Result;
import com.heihx.service.OrderService;
import com.heihx.vo.OrderStatisticsVO;
import com.heihx.vo.OrderVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Tag(name = "订单相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @Operation(summary = "订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @GetMapping("/statistics")
    @Operation(summary = "各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }
    /**
     * 订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @Operation(summary = "订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id) {
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }
    /**
     * 接单
     *
     * @return
     */
    @PutMapping("/confirm")
    @Operation(summary = "接单")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     * @return
     */
    @PutMapping("/rejection")
    @Operation(summary = "拒单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 取消订单
     * @return
     */
    @PutMapping("/cancel")
    @Operation(summary = "取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @return
     */
    @PutMapping("/delivery/{id}")
    @Operation(summary = "派送订单")
    public Result delivery(@PathVariable("id") Long id) {
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @return
     */
    @PutMapping("/complete/{id}")
    @Operation(summary = "完成订单")
    public Result complete(@PathVariable("id") Long id) {
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 统计订单
     * @param begin
     * @param end
     * @param status
     * @return
     */
    @GetMapping("/countByMap")
    @Operation(summary = "统计订单")
    public Result<Long> countByMap(LocalDateTime begin, LocalDateTime end, Integer status){
        Long count = orderService.lambdaQuery()
                .gt(begin != null, Orders::getOrderTime, begin)
                .lt(end != null, Orders::getOrderTime, end)
                .eq(status != null, Orders::getStatus, status)
                .count();
        return Result.success(count);
    }

    /**
     * 统计订单金额
     * @param begin
     * @param end
     * @param status
     * @return
     */
    @GetMapping("/sumByMap")
    @Operation(summary = "统计订单金额")
    public Result<BigDecimal> sumByMap(LocalDateTime begin, LocalDateTime end, Integer status
    ){
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.select("IFNULL(sum(amount),0) as total");
        wrapper.gt(begin != null, "order_time", begin);
        wrapper.lt(end != null, "order_time", end);
        wrapper.eq(status != null,"status", status);
        Map<String, Object> map = orderService.getMap(wrapper);
        BigDecimal turnover = (BigDecimal)map.get("total");
        return Result.success(turnover);
    }

    /**
     * 查询销量top10
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/top10")
    @Operation(summary = "查询销量top10")
    public Result<List<GoodsSalesDTO>> top10(LocalDateTime begin, LocalDateTime end){
        return Result.success(orderService.top10(begin, end));
    }

}
